package com.shuoyue.ycgk.ui.mine.mynote;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.ChapterType;
import com.shuoyue.ycgk.entity.PersonNote;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.mine.mynote.adapter.ChapterAdapter;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;

public class FragmentPraisedTab extends BaseMvpFragment<MyNoteContract.Presenter>
        implements MyNoteContract.View {
    Type type;
    String title;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    ChapterAdapter chapterAdapter;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    @Override
    public void initParams(Bundle bundle) {
        type = (Type) bundle.getSerializable("type");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mynote;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getType(type.getId(), 1);
            refreshLayout.finishRefresh(2000);
        });
        chapterAdapter = new ChapterAdapter(null);
        recyclerView.setAdapter(chapterAdapter);
        chapterAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ChapterType specialType = (ChapterType) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_title:
                    specialType.setSelect(!specialType.isSelect());
                    adapter.notifyDataSetChanged();
                    break;
            }
        });
        chapterAdapter.setDoquestions((sectionId, name) -> {
            MineNoteListActivity.start(mActivity, sectionId, 1, name);
        });
        registEmptyView(chapterAdapter);
    }

    @Override
    protected void initData() {
        mPresenter = new MyNoteContract.Presenter();
        mPresenter.attachView(this);
//        wrongAdapter.resetData(type.getChildren());
        mPresenter.getType(type.getId(), 1);
    }


    public static FragmentPraisedTab getInstance(Type type) {
        FragmentPraisedTab fragment = new FragmentPraisedTab();
        fragment.title = type.getName();
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setTypeList(List<ChapterType> chapterSectionTypes) {
        chapterAdapter.resetData(chapterSectionTypes);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setQuestions(List<PersonNote> noteList) {

    }

    @Override
    public void praiseResult(PersonNote note, boolean isPraised) {

    }

    @Override
    public void deleteSuc(PersonNote questionNote) {

    }


    @Override
    public String getTitle() {
        return title;
    }
}
